﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Lite.Common;
using Photon.SocketServer;


namespace MyApplication
{
    public class EchoManager : IDisposable
    {
        private static EchoManager mSingle = new EchoManager();
        private EchoManager()
        { }

        public static EchoManager Single
        {
            get { return mSingle; }
        }

        Timer mTimer;
        public void StartEcho()
        {
            StopEcho();
            //1s
            mTimer = new Timer(new TimerCallback(TimerCallback), null, 0, 1000);
        }

        public void StopEcho()
        {
            if (mTimer != null)
            {
                mTimer.Dispose();
                mTimer = null;
            }
        }

        private void TimerCallback(object state)
        {
            Dictionary<byte, object> data = new Dictionary<byte, object>();
            data.Add((byte)MyParameterCodes.Response, DateTime.Now.ToFileTime());
            OperationResponse operRes = new OperationResponse((byte)MyOperationCodes.EchoOperation, data);
            SendParameters SendParameters = new SendParameters();
            foreach (PeerBase peer in PeerManager.Single.GetPeers())
            {
                peer.SendOperationResponse(operRes, SendParameters);
            }
        }

        public void Dispose()
        {
            StopEcho();
        }
    }
}
